using System;
using System.Xml;
using Portal.Core;

namespace Portal.Data.Entity
{
    public class Rss
    {
        private const string BREAK_LINE = "\r\n";
        public struct RssChannel
        {
            public string Title;
            public string Link;
            public string Description;
        }
        public struct RssItem
        {
            public string Title;
            public string Link;
            public string Description;
            public string PubDate;
        }
        private static XmlDocument addRssChannel(XmlDocument xmlDocument, RssChannel channel)
        {
            XmlElement channelElement = xmlDocument.CreateElement("channel");
            channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement titleElement = xmlDocument.CreateElement("title");
            titleElement.InnerText = channel.Title;
            channelElement.AppendChild(titleElement);
            channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement descriptionElement = xmlDocument.CreateElement("description");
            descriptionElement.InnerText = channel.Description;
            channelElement.AppendChild(descriptionElement);
            channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement linkElement = xmlDocument.CreateElement("link");
            linkElement.InnerText = channel.Link;
            channelElement.AppendChild(linkElement);
            channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            // Generator information
            XmlElement generatorElement = xmlDocument.CreateElement("generator");
            generatorElement.InnerText = CoreContext.basepath;
            channelElement.AppendChild(generatorElement);
            channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement pubDateElement = xmlDocument.CreateElement("lastBuildDate");
            pubDateElement.InnerText = DateTime.Now.ToString("r");
            channelElement.AppendChild(pubDateElement);
            channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlNode rssElement = xmlDocument.SelectSingleNode("rss");
            if (rssElement != null)
            {
                rssElement.AppendChild(channelElement);
                rssElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));
            }

            return xmlDocument;
        }

        private static XmlDocument addRssItem(XmlDocument xmlDocument, RssItem item)
        {
            XmlNode channelElement = xmlDocument.SelectSingleNode("rss/channel");

            //Create the item
            XmlElement itemElement = xmlDocument.CreateElement("item");
            itemElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement titleElement = xmlDocument.CreateElement("title");
            titleElement.InnerText = item.Title;
            itemElement.AppendChild(titleElement);
            itemElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement linkElement = xmlDocument.CreateElement("link");
            linkElement.InnerText = item.Link;
            itemElement.AppendChild(linkElement);
            itemElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement pubdateElement = xmlDocument.CreateElement("pubDate");
            pubdateElement.InnerText = item.PubDate;
            itemElement.AppendChild(pubdateElement);
            itemElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            XmlElement descriptionElement = xmlDocument.CreateElement("description");
            descriptionElement.InnerXml = "<![CDATA[ " + item.Description + " ]]>";
            itemElement.AppendChild(descriptionElement);
            itemElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));

            //append this item
            if (channelElement != null)
            {
                channelElement.AppendChild(itemElement);
                channelElement.AppendChild(xmlDocument.CreateWhitespace(BREAK_LINE));
            }

            return xmlDocument;
        }

        private XmlDocument _rss;
        public Rss()
        {
            _rss = new XmlDocument();

            XmlDeclaration xmlDeclaration = _rss.CreateXmlDeclaration("1.0", "utf-8", null);
            _rss.InsertBefore(xmlDeclaration, _rss.DocumentElement);
            _rss.AppendChild(_rss.CreateWhitespace(BREAK_LINE));
//
//            XmlProcessingInstruction newPi = _rss.CreateProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/RSS/ePiRSS.xsl'");
//            _rss.AppendChild(newPi);
//            _rss.AppendChild(_rss.CreateWhitespace(BREAK_LINE));

            XmlElement rssElement = _rss.CreateElement("rss");
            XmlAttribute xmlAtt = _rss.CreateAttribute("version");
            xmlAtt.InnerText = "2.0";
            rssElement.Attributes.Append(xmlAtt);
            rssElement.AppendChild(_rss.CreateWhitespace(BREAK_LINE));

            _rss.AppendChild(rssElement);
            _rss.AppendChild(_rss.CreateWhitespace(BREAK_LINE));
        }

        public void AddRssChannel(RssChannel channel)
        {
            _rss = addRssChannel(_rss, channel);
        }

        public void AddRssItem(RssItem item)
        {
            _rss = addRssItem(_rss, item);
        }

        /// <summary>
        /// Write string out.\r\n
        /// Using: Response.Clear();\r\n
        /// Response.ContentType = "text/xml";\r\n
        /// Response.Write(...);\r\n
        /// Response.End();
        /// </summary>
        public string RssDocument
        {
            get
            {
                return _rss.OuterXml;
            }
        }
        /// <summary>
        /// Write out XmlDocument type
        /// </summary>
        public XmlDocument RssXMLDocument
        {
            get
            {
                return _rss;
            }
        }

    }
}
